/**
 * \file Tree.h
 *
 * Asteriskos Compiler 1
 * Written 2010-2011, by Anthony Berlin and Alexander Carlqvist
 *
 * This file is part of ICC.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef TREE_H
#define TREE_H

#include "TreeType.h"
#include "SourceLine.h"
#include "Token.h"
#include "ValueTable.h"
#include "Symbol.h"

#include <GCB/Datatype.h>
#include <GCB/Object.h>

#include <fstream>

#ifndef NULL_TREE
    #define NULL_TREE (Tree*)NULL
#endif

class Tree : public Object
{
public:
    /// Constructors for 'Tree'.
    Tree(SourceLine* line, Token* token, TreeType type);
    Tree();

    /// Init some default values for data members.
    void init();

    /// Clone this tree, returning the copy.
    /// \note Only use this on a relatively small tree.
    Tree* clone(bool shallow = false);

    /// Push a sibling tree node onto the stack.
    void push(Tree* node, bool pushFront = false);

    /// Pop a sibling tree node from the stack.
    void pop(Tree* node);

    /// Return the first node of the stack (the one that got pushed onto the stack first).
    Tree* front() const;

    /// Return the last node of the stack (the one that got pushed onto the stack last).
    Tree* back() const;

    /// Return a node by index from the stack, counting from first added to last.
    Tree* get_node(unsigned int index) const;

    /// Return a string-representation of the tree type.
    string convert_type_to_string() const;

    /// Return a string-representation of the tree node.
    string to_string() const;

    /// Dump a text-representation of this tree to disc.
    bool dump_to_disc(std::ofstream& fileStream) const;

    /// Set which symbol this tree is associated with (if any).
    void set_associated_symbol(Symbol* symbol);

    /// The type of tree this is.
    TreeType m_type;

    /// The line, that the token this tree node was formed from, was found inside of.
    SourceLine* m_line;

    /// The token that this tree node was formed from.
    Token* m_token;

    /// The parent node of this tree node.
    Tree* m_parent;

    /// True if this is a function body.
    bool m_isFunctionBody;

    /// True if this is an operator.
    bool m_isOperator;

    /// True if this is a binary operator.
    bool m_isBinaryOperator;

    /// True if this expression could be considered an lvalue.
    bool m_isLValue;

    /// True if this is a specifier.
    bool m_isSpecifier;

    /// True if this is a modifier.
    bool m_isModifier;

    /// True if this is a constant expression of some kind.
    bool m_isConstant;

    /// Datatype. Used to determine datatypes of expressions.
    Datatype* m_datatype;

    /// Reserved identifier. Tells us which keyword this tree node is representing.
    TokenId m_rid;

    /// The stack of siblings for this tree node.
    list<Tree*>* m_stack;

private:
    /// A reference that is unique to this tree node.
    long long m_ref;

    /// The symbol this tree is associated with, if any.
    Symbol* m_symbol;
};

#endif // TREE_H
